package user

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	nhttp "net/http"
	"wzgames/internal/admin/adc"
	"wzgames/internal/admin/adsv/usersv"
	"wzgames/internal/admin/api/mid"
	"wzgames/internal/admin/api/wzabind"
	"wzgames/internal/admin/model"
	"wzgames/internal/response"
	"wzgames/pkg/logger"
	"wzgames/pkg/server/http"
	"wzgames/pkg/server/http/types"
	"wzgames/pkg/utils/stringutil"
)

func RegisterUserRoute(pGroup *gin.RouterGroup) {
	bizGroup := pGroup.Group("user")
	http.MethodMapper(bizGroup, types.GET)(userListFn())
	http.MethodMapper(bizGroup, types.GET)(userWalletsFn())
	http.MethodMapper(bizGroup, types.GET)(userWalletTransfersFn())
	http.MethodMapper(bizGroup, types.GET)(userGameLoginLogFn())
}

var userListFn = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/list")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm := adc.GetReceiver().GetAdmin(adminId)
		if adm == nil {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize})
			return
		}
		resp := model.Response{}
		var params wzabind.WzUserReq
		if err := c.ShouldBind(&params); err != nil {
			//logger.Error("err == ", zap.String("error", err.Error()))
			resp.Code = response.ErrBadRequest
			c.JSON(nhttp.StatusOK, resp)
			return
		}
		if params.Page <= 0 {
			params.Page = 1
		}
		if params.PageSize <= 0 {
			params.PageSize = 50
		}
		ret, code := usersv.SV().GetUserList(adm, params)
		resp.Code = code
		if code == 0 {
			resp.Data = gin.H{"data": ret}
		}

		c.JSON(nhttp.StatusOK, resp)
		return
	}
}

var userWalletsFn = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/wallets/:userId")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm := adc.GetReceiver().GetAdmin(adminId)
		if adm == nil {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize})
			return
		}
		userId, _ := stringutil.ToUint64(c.Param("userId"), 0)
		if userId <= 0 {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrBadRequest})
			return
		}

		resp := model.Response{}
		ret, code := usersv.SV().GetUserWallets(adm, userId)
		resp.Code = code
		if code == 0 {
			resp.Data = gin.H{"data": ret}
		}

		c.JSON(nhttp.StatusOK, resp)
		return
	}
}

var userWalletTransfersFn = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/wallet/transfers")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm := adc.GetReceiver().GetAdmin(adminId)
		if adm == nil {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize})
			return
		}
		resp := model.Response{}
		var params wzabind.WzWalletTransferReq
		if err := c.ShouldBind(&params); err != nil {
			logger.Error("err == ", zap.String("error", err.Error()))
			resp.Code = response.ErrBadRequest
			c.JSON(nhttp.StatusOK, resp)
			return
		}
		if params.Page <= 0 {
			params.Page = 1
		}
		if params.PageSize <= 0 {
			params.PageSize = 50
		}
		ret, code := usersv.SV().GetUserWalletTransfers(adm, params)
		resp.Code = code
		if code == 0 {
			resp.Data = gin.H{"data": ret}
		}

		c.JSON(nhttp.StatusOK, resp)
		return
	}
}

var userGameLoginLogFn = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/game/log/logins")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm := adc.GetReceiver().GetAdmin(adminId)
		if adm == nil {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize})
			return
		}
		resp := model.Response{}
		var params wzabind.WzUserReq
		if err := c.ShouldBind(&params); err != nil {
			logger.Error("err == ", zap.String("error", err.Error()))
			resp.Code = response.ErrBadRequest
			c.JSON(nhttp.StatusOK, resp)
			return
		}
		if params.Page <= 0 {
			params.Page = 1
		}
		if params.PageSize <= 0 {
			params.PageSize = 50
		}
		ret, code := usersv.SV().GetUserGameLoginLogs(adm, params)
		resp.Code = code
		if code == 0 {
			resp.Data = gin.H{"data": ret}
		}

		c.JSON(nhttp.StatusOK, resp)
		return
	}
}
